/**
 * yd tabber class
 * @description tested in IE_,IE7,FF_,FF3,Safari 3,Safari 4(beta),Google Chrome & Opera 9.6
 * @subpackage prototype
 * @author basil suter
 * @date 24-03-2008
 * @version v2.0a
 */
var ydtab = Class.create({
	
	initialize: function(tab_count,tab_wrap,nav_wrap,class_activ,class_inactiv){

		this.tab_count = $(tab_wrap).childElements().size();
		this.tab_wrap = tab_wrap;
		this.nav_wrap = nav_wrap;
		this.class_activ = class_activ;
		this.class_inactiv = class_inactiv;
		
		this.reload();
	},
	
	reload: function(){
		
		this.this_tab = 0;
		this.elmn_tab_wrap = $(this.tab_wrap).childElements();

		this.obsrv();
		this.drp(this.this_tab);
	},
	
	obsrv: function(){
		nav_wrap_arr = $$('#'+this.nav_wrap+' li');
		nav_wrap_arr.each(function (tog_this, index) {
		  tog_this.observe('click', function () {
            tabber.drp(index);
		  }.bind(this));
		});
	},
	
	drp: function(this_tab){
		this.this_tab = this_tab;
		this.elmn_tab_wrap.each(function(div){
			$(div).setStyle({
				display: 'none'
			});
		});

		this.activate();
		this.linktrigger();
	},
	
	activate: function(){
		$(this.elmn_tab_wrap)[this.this_tab].setStyle({
			display: 'block'
		});
	},
	
	linktrigger: function(){
		
		for(i=0;i<this.tab_count; i++){
			this_elmn = $$('#'+this.nav_wrap+' li a')[i];
			if(this.this_tab == i){
				$(this_elmn).className = this.class_activ;
			}else{
				$(this_elmn).className = this.class_inactiv;
			}
		}
		
	},
	
	getTab: function()
	{
		return $(this.elmn_tab_wrap)[this.this_tab];
	},
	
	addTab: function(title, content) 
	{
		var elmn = new Element("div", { 'class' : 'edit_tab' }).update(content);
		$(this.tab_wrap).insert(elmn);
		
		var nav = new Element("li").update("<a>"+title+"</a>");
		$(this.nav_wrap).insert(nav);
		
		ydtab_load();
		
		return $(this.nav_wrap).childElements().size() - 1;
	},
	
	closeTab: function(tab_index)
	{
		$$('#'+this.nav_wrap+' li').each(function(item, key){
			
			if(key == tab_index)
			{
				item.remove();
			}
			
		});
		
		childs = $(this.tab_wrap).childElements();
		
		childs.each(function(item, key){
			
			if(key == tab_index)
			{
				item.remove();
			}
			
		});
		
		this.reload();
	}
});

function ydtab_compare(wrap,nav){
	count_1 = $(wrap).childElements().size();
	count_2 = $(nav).childElements().size();
	if(count_1!=count_2){
		alert("YDTAB: TAB_NAV NOT EQUAL TAB_WRAP");
	}else{
		return count_1;
	}
}

function ydtab_load()
{
	/*
	 * ydtab config params
	 * @param tabs wrapper id
	 * @param navigation wrapper id
	 * @param navigation activ class
	 * @param navigation inactiv class
	 */
	ydtab_wrapper 		= 'tab_wrap';
	ydtab_navigator 	= 'tab_nav';
	ydtab_class_activ 	= 'tab_active';
	ydtab_class_inactiv	= 'tab_inactive';

	if(document.getElementById(ydtab_wrapper) && document.getElementById(ydtab_navigator)){

		tabber = new ydtab(ydtab_compare(ydtab_wrapper,ydtab_navigator),ydtab_wrapper,ydtab_navigator,ydtab_class_activ,ydtab_class_inactiv);
	}
}

document.observe('dom:loaded', ydtab_load);